home *** CD-ROM | disk | FTP | other *** search
/ ASME's Mechanical Engine…ing Toolkit 1997 December / ASME's Mechanical Engineering Toolkit 1997 December.iso / c_lang / z150_src.lzh / ZOODEL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-07-12  |  5.0 KB  |  145 lines

  1. #ifndef LINT
  2. static char sccsid[]="@(#) zoodel.c 1.8 87/05/29 12:56:02";
  3. #endif /* LINT */
  4.  
  5. /*
  6. Copyright (C) 1986, 1987 Rahul Dhesi -- All rights reserved
  7. */
  8. #include "options.h"
  9. /* Deletes or undeletes entries from an archive.  choice=1 requests
  10.    deletion and choice=0 requests undeletion. */
  11. #include "portable.h"
  12. #include <stdio.h>
  13. #include "various.h" /* may not be needed */
  14. #include "zoo.h"
  15. #include "zoofns.h"
  16. #include "errors.i"
  17.  
  18. #ifndef NOSIGNAL
  19. #include <signal.h>
  20. #endif
  21.  
  22. extern int quiet;
  23.  
  24. void zoodel (zoo_path, option, choice)
  25. char *zoo_path;
  26. char *option;
  27. int choice;
  28. {
  29. #ifndef NOSIGNAL
  30.    int (*oldsignal)();        /* to save previous SIGINT handler */
  31. #endif
  32.    int delcount = 0;          /* how many entries we [un]deleted */
  33.    char matchname[PATHSIZE];  /* will hold full pathname */
  34.    register FILE *zoo_file;
  35.    struct zoo_header zoo_header;
  36.    struct direntry direntry;
  37.    unsigned int latest_date = 0;      /* so we can set time of archive later */
  38.    unsigned int latest_time = 0;
  39.    int pack = 0;              /* pack after deletion? */
  40.    int file_deleted = 0;      /* any files deleted? */
  41.    int one = 0;               /* del/undel one file only */
  42.    int done;                  /* loop control */   
  43. while (*(++option)) {
  44.    switch (*option) {
  45.       case 'P': pack++; break;            /* pack after adding */
  46.       case 'q': quiet++; break;           /* be quiet */
  47.       case '1': one++; break;             /* del or undel only one file */
  48.       default:
  49.          prterror ('f', inv_option, *option);
  50.    }
  51. } /* end while */
  52.  
  53.    /* Open archive for read/write/binary access.  It must already exist */
  54.    if ((zoo_file = fopen (zoo_path, FRWSTR)) == NULL) {
  55.       prterror ('f', could_not_open, zoo_path);
  56.    }
  57.    
  58.    /* read archive header */
  59.    frd_zooh (&zoo_header, zoo_file);
  60.    /* fread ((char *) &zoo_header, sizeof(zoo_header), 1, zoo_file); */
  61.    if ((zoo_header.zoo_start + zoo_header.zoo_minus) != 0L)
  62.       prterror ('f', failed_consistency);
  63.    fseek (zoo_file, zoo_header.zoo_start, 0); /* seek to where data begins */
  64.    
  65.    done = 0;            /* loop not done yet */
  66.    /* Go into loop deleting requested entries */
  67.    while (1) {
  68.       long this_dir_offset;
  69.       this_dir_offset = ftell (zoo_file);    /* save pos'n of this dir entry */
  70.       frd_dir (&direntry, zoo_file);
  71.       /* fread ((char *) &direntry, sizeof (direntry), 1, zoo_file); */
  72.       if (direntry.zoo_tag != ZOO_TAG) {
  73.          prterror ('f', bad_directory);
  74.       }
  75.       if (direntry.next == 0L) {                /* END OF CHAIN */
  76.          break;                                 /* EXIT on end of chain */
  77.       }
  78.       
  79.       /* Now [un]delete this entry if it isn't already [un]deleted and 
  80.          if filename matches.  WARNING: convention of choice=1 for
  81.          deleted entry must be same as in direntry definition in zoo.h */
  82.    
  83.       /* Test for "done" so if "one" option requested, [un]del only 1 file */
  84.       /* But we go through the whole archive to adjust archive time */
  85.  
  86.         strcpy (matchname, fullpath (&direntry));        /* get full pathname */
  87.  
  88.       if (!done && direntry.deleted != choice && 
  89.                                              needed(matchname)) {
  90.          prterror ('m', "%-14s -- ", matchname);
  91.          delcount++;
  92.          direntry.deleted = choice;
  93.          if (choice) 
  94.             file_deleted++;      /* remember if any files actually deleted */
  95.  
  96.          fseek (zoo_file, this_dir_offset, 0);
  97.  
  98. #ifndef NOSIGNAL
  99.          oldsignal = signal (SIGINT, SIG_IGN);  /* disable ^C for write */
  100. #endif
  101.          if (fwr_dir (&direntry, zoo_file) == -1)
  102.          /* if (fwrite ((char *) &direntry, sizeof(direntry), 1, zoo_file) < 1) */
  103.             prterror ('f', "Could not write to archive\n");
  104. #ifndef NOSIGNAL
  105.          signal (SIGINT, oldsignal);
  106. #endif
  107.          prterror ('M', choice ? "deleted\n" : "undeleted\n");
  108.          if (one)
  109.             done = 1;            /* if 1 option, done after 1 file */
  110.       }
  111.  
  112.       /* remember most recent date and time if entry is not deleted */
  113.       if (!direntry.deleted)
  114.          if (direntry.date > latest_date ||
  115.             (direntry.date == latest_date && direntry.time > latest_time)) {
  116.                latest_date = direntry.date;
  117.                latest_time = direntry.time;
  118.          }
  119.       fseek (zoo_file, direntry.next, 0); /* ..seek to next dir entry */
  120.    } /* endwhile */
  121.  
  122.    if (!delcount)
  123.       printf ("Zoo:  No files matched.\n");
  124.    else {
  125. #ifdef NIXTIME
  126.       fclose (zoo_file);
  127.       setutime (zoo_path, latest_date, latest_time);
  128. #else
  129.       fflush (zoo_file);         /* superstition:  might help time stamp */
  130.       settime (fileno(zoo_file), latest_date, latest_time);
  131. #endif
  132.    }
  133.  
  134. #ifndef NIXTIME
  135. fclose (zoo_file);
  136. #endif
  137.  
  138. if (file_deleted && pack) {   /* pack if files were deleted and user asked */
  139.    prterror ('M', "-----\nPacking...");
  140.    zoopack (zoo_path, "PP");
  141.    prterror ('M', "done\n");
  142. }
  143.  
  144. }
  145.